Array

  • STEP
    Array variables allow us to group related PHP data values together in a list using a single variable name. We can store multiple data in a single variable.

    1. Define variable or variable assignment

    Let us define separate variable for storing data of students , teachers and staff

    
                  <?php 
    
                  $name="manoj";
                  $age=20;
                  $dob="20-01-2020";
                  $english=50;
                  $malayalam=50;
                  $computer=50;
                  $maths=50;
                  $gk=50;
    
    
    
                  $teachers_name="manoj";
                  $teachers_age=20;
                  $teachers_dob="20-01-2020";
                  $basic=25000;
                  $hra=5000;
                  $ta=100;
    
    
    
                  $staff_name="manoj";
                  $staff_age=20;
                  $staff_dob="20-01-2020";
                  $staff_basic=25000;
                  $staff_hra=5000;
                  $staff_ta=100;
    
    
                 
    
                  ?>
    
                

    The above method is not good for managing or maintaining the variables while developing an application

    2. Group the related data and store in a single variable using array

    Syntax

    
                    $variable_name = [];
    
                    OR
    
                    $variable_name = ['key' => 'value'];
    
                
    1. $ sign must be in front of the variable name
    2. = sign for assigning the value to a variable
    3. => sign for separating key and value in an array
    4. , sign for separating the multiple values in an array
    
                  <?php 
    
                  $student=["name" => "manoj", "age" => 20, "dob" => "20-01-2020", "english" => 50, "malayalam" => 60, "computer" => 70, "maths" => 80, "gk" => 90];     
                  
                  // OR    
                  $student["name"]="manoj";
                  $student["age"]=20;   
    
    
                  $teacher=["name" => "manoj",  "age" => 20, "dob" => "20-01-2020", "basic" => 25000, "hra" => 5000, "ta" => 100]; 
                  
                  // OR 
                  $teacher["name"]="manoj";
                  $teacher["age"]=20;
    
    
    
                  $staff=["name" => "manoj", "age" => 20, "dob" => "20-01-2020", "basic" => 25000, "hra" => 5000, "ta" => 100];
    
                  // OR 
                  $staff["name"]="manoj";
                  $staff["age"]=20;
    
                  ?>
    
                

    3. Read the array data with key

    
                  <?php 
    
                  $student["name"];   
                  $student["age"];  
                  $student["dob"];         
    
                  $teacher["name"]; 
                  $teacher["age"]; 
                  $teacher["dob"]; 
    
                  $staff["name"];
                  $staff["age"];
                  $staff["dob"];
    
                  ?>
    
                

    4. update array data with key

    
                  <?php 
    
                  $student["name"]="manoj1";   
                  $student["age"]=25;  
                  $student["dob"]="20-01-2020"; 
                  
                  
                  $teacher["name"]="manoj1";   
                  $teacher["age"]=25;  
                  $teacher["dob"]="20-01-2020";      
    
            
                  ?>
    
                

    5. output to the browser using echo

    
                <?php 
    
               echo  $student["name"];   
               echo $student["age"];  
               echo $student["dob"];         
    
               echo  $teacher["name"]; 
               echo  $teacher["age"]; 
               echo  $teacher["dob"]; 
    
               echo $staff["name"];
               echo  $staff["age"];
               echo  $staff["dob"];
    
                ?>
    
              

    6. Show the data user friendly

    
                    <table>
                        <tr>
                            <td>Student Name</td>
                            <td> 
                                <?php                              
                                  echo  $student["name"]; 
                                ?>
                            </td>
                      </tr>
    
                      <tr>
                            <td>Student Age</td>
                            <td> 
                                <?php                              
                                  echo  $student["age"]; 
                                ?>
                            </td>
                      </tr>
    
                      <tr>
                            <td>Student DOB</td>
                            <td> 
                                <?php                              
                                  echo $student["dob"]; 
                                ?>
                            </td>
                      </tr>
    
    
    
                    </table>
    
    
    
                    <table>
                        <tr>
                            <td>Teacher Name</td>
                            <td> 
                                <?php                              
                                  echo  $teacher["name"]; 
                                ?>
                            </td>
                      </tr>
    
                      <tr>
                            <td>Teacher Age</td>
                            <td> 
                                <?php                              
                                  echo  $teacher["age"]; 
                                ?>
                            </td>
                      </tr>
    
                      <tr>
                            <td>Teacher DOB</td>
                            <td> 
                                <?php                              
                                  echo $teacher["dob"]; 
                                ?>
                            </td>
                      </tr>
    
    
    
                    </table>
    
    
    
    
                    <table>
                        <tr>
                            <td>Staff Name</td>
                            <td> 
                                <?php                              
                                  echo  $staff["name"]; 
                                ?>
                            </td>
                      </tr>
    
                      <tr>
                            <td>Staff Age</td>
                            <td> 
                                <?php                              
                                  echo  $staff["age"]; 
                                ?>
                            </td>
                      </tr>
    
                      <tr>
                            <td>Staff DOB</td>
                            <td> 
                                <?php                              
                                  echo $staff["dob"]; 
                                ?>
                            </td>
                      </tr>
    
    
    
                    </table>